package per.kongtouxu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import per.kongtouxu.Util.CommonMethod;
import per.kongtouxu.domain.dto.JsonDto;
import per.kongtouxu.domain.pojo.Admin;
import per.kongtouxu.domain.pojo.AdminLog;
import per.kongtouxu.domain.pojo.PageBean;
import per.kongtouxu.domain.pojo.User;
import per.kongtouxu.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //分页查询
    @RequestMapping("/findUserByPage")
    public @ResponseBody PageBean<User> findUserByPage(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //1.获取参数
        String account = req.getParameter("account");
        String Tel = req.getParameter("Tel");
        String email = req.getParameter("email");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        //2.调用service
        PageBean<User> pageBean = userService.findByPage(account, Tel, email, startTime, endTime, Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        //3.返回JSON
        return pageBean;
    }
    //禁用启用
    @RequestMapping("/banOrEnable")
    public @ResponseBody Integer banOrEnable(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //1.获取参数
        String userName = req.getParameter("userName");
        String userState = req.getParameter("userState");
        Integer userStateId =null;
        //2.后台日志插入
        //2.1获取adminId
        //3.编写日志类
        AdminLog adminLog = CommonMethod.getAdminLog(req);
        if(userState.equals("禁用")){
            //设置为启用
            userStateId = 1;
            adminLog.setDoName("启用前台用户");
        }else if(userState.equals("启用")){
            //设置为禁用
            userStateId = 0;
            adminLog.setDoName("禁用前台用户");
        }
        //2.调用service
        Integer result = userService.updateUserStateByAccount(userName, userStateId,adminLog);
        //3.返回JSON
        return result;
    }
    //修改密码
    @RequestMapping("/alterPwd")
    public JsonDto alterPwd(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        //1.获取参数
        String account = req.getParameter("account");
        String olePwd = req.getParameter("olePwd");
        String newPwd = req.getParameter("newPwd");
        //2.返回的信息
        JsonDto info = new JsonDto();
        //2.后台日志插入
        //2.1获取adminId
        //3.编写日志类
        AdminLog adminLog = CommonMethod.getAdminLog(req);
        adminLog.setDoName("修改前台用户密码");
        //3.查询旧密码是否输入正确
        User user = userService.findUserByUP(account, newPwd);
        if(user !=null){
            //旧密码输入不正确
            info.setId(0);
            info.setMessage("旧密码输入错误，请重新输入");
        }else if (user == null){
            //旧密码输入正确,修改密码
            userService.updateUserPwdByAccount(account,newPwd,adminLog);
            info.setId(1);
            info.setMessage("密码修改成功");
        }
        //4.返回JSON
        return info;
    }
}
